//index.js
//获取应用实例
const app = getApp()

let videoAd = qq.createRewardedVideoAd({
    adUnitId: '3e3797d85807ebcbe6a77756f65ec362'
})

Page({
    data: {
        date: '2018-12-25',
        begin_date: null,
        endTime: '2025-12-31',
        multiArray: [
            ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"],
            ['00','10','20', '30','40','50']
        ],
        multiIndex: [9, 0],
        noteNowLen: 0,
        noteMaxLen: 1314,
        email: null,
        title: null,
        content:null,
        emailRight: 0,
        mail_show:1

    },

    onLoad: function () {
        var day3 = new Date();
        day3.setTime(day3.getTime() + 24 * 60 * 60 * 1000);
        var startTime = day3.getFullYear() + "-" + ((day3.getMonth() + 1)<10?'0'+(day3.getMonth() + 1):(day3.getMonth() + 1)) + "-" + (day3.getDate() <10?'0'+day3.getDate():day3.getDate());
        day3.setFullYear(day3.getFullYear()+10);
        day3.setTime(day3.getTime() - 24 * 60 * 60 * 1000);        
        var endTime = day3.getFullYear() + "-" + ((day3.getMonth() + 1)<10?'0'+(day3.getMonth() + 1):(day3.getMonth() + 1)) + "-" + (day3.getDate() <10?'0'+day3.getDate():day3.getDate());
        var multiIndex = [day3.getHours(),Math.floor(day3.getMinutes()/10)];


        this.setData({
            begin_date: startTime,
            date:startTime,
            endTime:endTime,
            multiIndex:multiIndex
        })
    },
    timeChange(e) {
        console.log(e)
        this.setData({
            multiIndex: e.detail.value
        })
    },
    dateChange(e) {
        console.log(e)
        this.setData({
            date: e.detail.value
        })
    },
    inputEmail(e) {
        let that = this
        var emailtest = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/
        if (emailtest.test(e.detail.value)) {
            that.setData({
                email: e.detail.value,
                emailRight: 2
            })
        } else {
            that.setData({
                email: e.detail.value,
                emailRight: 1
            })
        }
    },
    inputTitle(e) {
        this.setData({
            title: e.detail.value
        })
    },
    onChangeMailShow(e){
        this.setData({
            mail_show:e.detail.value?1:0
        })
    },
    bindTextAreaChange: function (e) {
        var that = this
        var value = e.detail.value,
            len = parseInt(value.length);
        if (len > that.data.noteMaxLen)
            return;
        that.setData({
            content: value,
            noteNowLen: len
        })
    },
    uploadMessage() {
        let data = Object();
        let that = this
        data.email = that.data.email
        data.openid = app.globalData.dbInfo.openid
        data.title = that.data.title
        data.agreed_time = that.data.date + ' ' + that.data.multiArray[0][that.data.multiIndex[0]] + ":" + that.data.multiArray[1][that.data.multiIndex[1]]
        data.content = that.data.content
        data.mail_show = that.data.mail_show
        data.userid = app.globalData.dbInfo.id
        console.log(data)
        if (that.data.emailRight != 2) {
            wx.showToast({
                title: "请正确输入邮箱地址！",
                icon: "none"
            })
        } else if (data.title == null || data.title == "") {
            wx.showToast({
                title: "请输入标题！",
                icon: "none"
            })

        } else if (data.content == null || data.content == "") {
            wx.showToast({
                title: "请输入正文！",
                icon: "none"
            })
        } else if (app.globalData.dbInfo.send_times < 1) {
            wx.showModal({
                title: "无邮件发送次数",
                content: "观看15秒广告可继续发送！",
                success(res) {
                    if (res.confirm) {
                        var i = 0;
                        wx.showLoading({
                            title: "加载中..."
                        })

                        let dbInfo = app.globalData.dbInfo

                        videoAd.onError(function (res) {
                            wx.hideLoading()
                            if (i == 0) {
                                i++
                                wx.showToast({
                                    title: "环境异常，请重试！",
                                    icon: "none"
                                })
                            }


                        })
                        videoAd.onLoad(function (res) {
                            wx.hideLoading()
                            console.log('videoAd onLoad', res)
                        })
                        videoAd.onClose(function (res) {
                            console.log('videoAd onClose', res)
                            if (res.isEnded && i == 0) {
                                i++
                                wx.showLoading({
                                    title: "正在添加次数..."
                                })

                                app.httpPost('index/adAddTimes', { id: dbInfo.id, openid: dbInfo.openid }, function (res) {
                                    wx.hideLoading()
                                    console.log(res)
                                    if (res.status == 1) {
                                        dbInfo.send_times = res.send_times
                                        wx.showToast({
                                            title: '次数增加成功！',
                                            icon: 'success'
                                        })
                                        app.globalData.dbInfo = dbInfo
                                    }else if (res.status == 0) {
                                        wx.showToast({
                                            title: res.msg,
                                            icon: "none"
                                        })
                                    } else {
                                        wx.showToast({
                                            title: "系统错误！请联系管理员",
                                            icon: "none"
                                        })
                                    }

                                })
                            } else if (res.isEnded) {
                                //多次跳出
                            } else {

                                wx.showModal({
                                    title: "视频未看完"

                                })


                            }
                        })

                        videoAd.load()
                            .then(() => {
                                console.log('激励视频加载成功');
                                videoAd.show().then(() => {
                                    console.log('激励视频 广告显示成功')
                                })
                                    .catch(err => {
                                        console.log('激励视频 广告显示失败')
                                    })
                            })
                            .catch(err => {
                                console.log('激励视频加载失败');
                            })
                    } else if (res.cancel) {

                    }
                }


            })


        } else {
            wx.showLoading({
                title: "正在上传..."
            })

            app.httpPost("index/addNewLetter", data, function (res) {
                wx.hideLoading()
                if (res.status == 1) {
                    wx.showToast({
                        title: res.msg
                    })
                    that.setData({
                        email:null,
                        title:null,
                        content:null,
                        noteNowLen:0,
                        emailRight:0
                    })

                    let dbInfo = app.globalData.dbInfo
                    dbInfo.send_times = res.send_times
                    app.globalData.dbInfo = dbInfo
                } else if (res.status == 0) {
                    wx.showToast({
                        title: res.msg,
                        icon: "none"
                    })
                } else {
                    wx.showToast({
                        title: "服务器异常！",
                        icon: "none"
                    })
                }


            })


        }


    }
})
